//Copyright (c) 2007 Andrew Badera andrew@badera.us
//SVN repository at http://powncenet.googlecode.com/svn/

//Permission is hereby granted, free of charge, to any person
//obtaining a copy of this software and associated documentation
//files (the "Software"), to deal in the Software without
//restriction, including without limitation the rights to use,
//copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the
//Software is furnished to do so, subject to the following
//conditions:

//The above copyright notice and this permission notice shall be
//included in all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//OTHER DEALINGS IN THE SOFTWARE.


using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Windows.Forms;

using JsonFx.Json;

using BadException;
using BadException.Pownce;
using BadUtil;
using BadWeb;

namespace PownceNET
{
    /// <summary>
    /// Instance class for working with Pownce Notes.
    /// </summary>
    public class Note
    {
        private string _body = string.Empty;

        /// <summary>
        /// Gets or sets the note body.
        /// </summary>
        /// <value>The note body.</value>
        public string Body
        {
            get { return _body; }
            set { _body = value; }
        }

        private string _recipient = string.Empty;

        /// <summary>
        /// Gets or sets the note recipient.
        /// </summary>
        /// <value>The recipient; public, all (all friends), set_##, friend_###.</value>
        public string Recipient
        {
            get { return _recipient; }
            set { _recipient = value; }
        }

        /// <summary>
        /// Posts the note. Must construct elements of note first.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        public void PostNote()
        {
            if (string.IsNullOrEmpty(_body))
            {
                throw new ArgumentException("Requires note body.");
            }

            if (string.IsNullOrEmpty(_recipient))
            {
                throw new ArgumentException("Requires note recipient.");
            }

            if (string.IsNullOrEmpty(User.USERNAME))
            {
                throw new ArgumentException("Requires User.USERNAME.");
            }

            if (string.IsNullOrEmpty(User.PASSWORD))
            {
                throw new ArgumentException("Requires User.PASSWORD.");
            }

            string shortnonce = string.Empty;
            string timestamp = string.Empty;

			//get the WSSE digest
            string digest = Session.GetPownceWSSEDigest(ref shortnonce, ref timestamp);

            //get our actual WSSE UsernameToken
            string authToken = WSSE.GetAuthToken(User.USERNAME, digest, shortnonce, timestamp);

            //build a list of headers
            //not currently using it obviously
            List<HttpHeader> headers = new List<HttpHeader>();

            //originally had these implicitly encoded by the HttpVar object
            //while playing around with url vs post of auth value
            //I had removed this implicit encoding. Should, probably will
            //return to encoding within the data object.

            //build our Note
            HttpVar varType = new HttpVar(HttpUtility.UrlEncode("note_type"), HttpUtility.UrlEncode("note-message"));
            HttpVar varBody = new HttpVar(HttpUtility.UrlEncode("note_body"), HttpUtility.UrlEncode(this._body));
            HttpVar varTo = new HttpVar(HttpUtility.UrlEncode("note_to"), "public");

            //build our List of POST data values
            List<HttpVar> vars = new List<HttpVar>();
            vars.Add(varType);
            vars.Add(varBody);
            vars.Add(varTo);

            //let's try and post this baby
            try
            {
				string response = Post.DoPost(UrlConstants.POST_NOTE + HttpUtility.UrlEncode(authToken), vars, headers, HttpConstants.X_WWW_URLENCODED, "", "", Session.SessionID);
            }
            catch (Exception ex)
            {
                //something went wrong
                //log it
                BadLogger.WriteToLog("Note.PostNote{Post.DoPost}", ex, ExceptionEnums.LogEntryType.Error);
                throw new PostNoteException("Could not post note.", ex);
            }
        }
    }
}
